package me.zhengjie.dailyPoem.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import me.zhengjie.dailyPoem.mapper.PoemRecommendMapper;
import me.zhengjie.dailyPoem.service.PoemRecommendService;
import me.zhengjie.poemPoet.domain.PoemPoet;
import me.zhengjie.poemmanager.domain.Query;
import me.zhengjie.poemmanager.domain.UserInfoCount;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class PoemRecommendServiceIpl implements PoemRecommendService {
    @Autowired
    private PoemRecommendMapper poemRecommendMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public PoemPoet poemRecommend() {
        return poemRecommendMapper.poemRecommend();
    }

    public PoemPoet getDailyPoem() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 将日期转换为长整型作为随机数种子
        long seed = currentDate.toEpochDay();
        // 创建随机数生成器并设置种子
        Random random = new Random(seed);
        // 生成 1 到 60 之间的随机整数作为 poemId
        int poemId = random.nextInt(60) + 1;
        // 调用 Mapper 接口查询诗歌
        return poemRecommendMapper.getDailyPoem(poemId);
        }

    public List<PoemPoet> getRelatedPoem() {
        return poemRecommendMapper.getRelatedPost();
    }

    public UserInfoCount getRealtedInfo(String userName) {
        return poemRecommendMapper.getRealtedInfo(userName);
    }

    public List<Query> queryInfo(String keyWord) {
        return poemRecommendMapper.queryInfo(keyWord);
    }

    @Override
    public PageResult<Query> queryInfoPaged(String keyWord, Page<Object> page) {
        IPage<Query> iPage = poemRecommendMapper.queryInfoPaged(page, keyWord);
        return PageUtil.toPage(iPage);
    }

}

